package com.asset.dispoal.business.bsAgreement.service.commom;

import com.asset.dispoal.business.appoint.domain.db.mapper.AppointMapper;
import com.asset.dispoal.business.bsAgreement.domain.db.entity.AgreementCaseState;
import com.asset.dispoal.business.bsAgreement.domain.db.entity.AgreementStateRecord;
import com.asset.dispoal.business.bsAgreement.domain.db.mapper.AgreementMapper;
import com.asset.dispoal.business.bsCase.domain.db.entity.BsCase;
import com.asset.dispoal.business.bsCase.domain.db.mapper.BsCaseMapper;
import com.asset.dispoal.commom.menus.AgreementCaseSateMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tech.mhuang.pacebox.core.date.DateTimeUtil;
import tech.mhuang.pacebox.core.exception.BusinessException;
import tech.mhuang.pacebox.core.id.BaseIdeable;
import tech.mhuang.pacebox.springboot.core.local.GlobalHeaderThreadLocal;
import tech.mhuang.pacebox.springboot.protocol.Result;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Component
public class AgreementAbolishCaseCommand {

    @Autowired
    private AgreementMapper agreementMapper;

    @Autowired
    private AppointMapper appointMapper;

    @Autowired
    private BsCaseMapper caseMapper;

    @Autowired
    private BaseIdeable<String> ideable;

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Result execute(String id, String wasteReasonId) {
        String userId = GlobalHeaderThreadLocal.getOrException().getUserId();
        List<AgreementCaseState> agreementCaseStateList = agreementMapper.queryAgreementCaseStateByAgreementId(id);
        if (CollectionUtils.isEmpty(agreementCaseStateList)) {
            throw new BusinessException(500, "合同号已在废案池中！");
        }
        boolean delCase = true;
        String oldState = null;
        BigDecimal contractLawsuitPrincipalAmount = BigDecimal.ZERO;

        BigDecimal contractCreditPrincipalAmount = BigDecimal.ZERO;

        BigDecimal contractLawsuitInterestAmount = BigDecimal.ZERO;

        for (AgreementCaseState agreementCaseState : agreementCaseStateList) {
            if (id.equals(agreementCaseState.getAgreementId())) {
                contractLawsuitPrincipalAmount = agreementCaseState.getContractCreditPrincipalAmount();
                contractCreditPrincipalAmount = agreementCaseState.getContractCreditPrincipalAmount();
                contractLawsuitInterestAmount = agreementCaseState.getContractLawsuitInterestAmount();
                oldState = agreementCaseState.getCaseState();
                continue;
            }
            delCase = false;
        }
        List<String> list = new ArrayList<>();
        list.add(id);
        agreementMapper.deleteAgreementCaseByAgreementIds(list);
        String caseId = agreementCaseStateList.get(0).getCaseId();
        if (delCase) {
            //查询委派记录是否还有其他的信息，如果没有了则删除委派信息
            caseMapper.deleteRecordById(caseId);
            caseMapper.deleteById(caseId);
            appointMapper.deleteMapperByCaseId(caseId);
        } else {
            //扣除减少的合同金额
            BsCase cases = new BsCase();
            cases.setId(caseId);
            cases.setOperateUser(userId);
            cases.setOperateTime(DateTimeUtil.currentDate());
            cases.setLawsuitPrincipalAmount(contractLawsuitPrincipalAmount);
            cases.setCreditPrincipalAmount(contractCreditPrincipalAmount);
            cases.setLawsuitInterestAmount(contractLawsuitInterestAmount);
            caseMapper.subtraceAmount(cases);
        }

        agreementMapper.updateCaseStateAndReason(id, AgreementCaseSateMenu.ABOLISH_CASE.getLabel(), "1", userId, wasteReasonId);
        AgreementStateRecord agreementStateRecord = new AgreementStateRecord();
        agreementStateRecord.setId(ideable.generateId());
        agreementStateRecord.setContractId(id);
        agreementStateRecord.setCreateUser(userId);
        agreementStateRecord.setCreateTime(DateTimeUtil.currentDate());
        agreementStateRecord.setOldState(oldState);
        agreementStateRecord.setNewState(AgreementCaseSateMenu.ABOLISH_CASE.getLabel());
        agreementMapper.insertStateRecord(agreementStateRecord);
        return Result.ok();
    }
}
